# TODO selenium 体验
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#打开chrome浏览器
driver = webdriver.Chrome()

#浏览器最大化
driver.maximize_window()
time.sleep(2)

#输入网址-百度的网址
driver.get('https://www.baidu.com')

#在搜索输入关键字—天气预报
time.sleep(2)
kw = driver.find_element(By.ID,'kw')
kw.send_keys('天气预报')

#点击搜索按钮
time.sleep(2)
su = driver.find_element(By.ID,'su')
su.click()

#关闭浏览器
time.sleep(2)
driver.quit()

# TODO 各个浏览器的驱动使用
# TODO chrorme
#导入selenium
import time
from selenium import webdriver
#打开谷歌浏览器
driver = webdriver.Chrome()
#退出浏览器
time.sleep(2)
driver.quit()

# TODO firefox
#导入selenium
import time
from selenium import webdriver
#打开火狐浏览器
driver = webdriver.Firefox()
#退出浏览器
time.sleep(2)
driver.quit()

# TODO 第一个测试案例--面向过程01

# 导入模块
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

#1.打开谷歌浏览器,输入百度网址
driver = webdriver.Chrome()
time.sleep(3)
driver.get('https://www.baidu.com')

#2.获取搜索框并输入关键字--天气预报
time.sleep(3)
#定位搜索框
kw = driver.find_element(By.ID,'kw')
#输入关键字
kw.send_keys('天气预报')
#3.获取百度一下按钮
time.sleep(3)
su = driver.find_element(By.ID,'su')
#点击百度一下
su.click()
#4.获取页面标题，并打印出来
time.sleep(3)
print(driver.title)
#5.检查'python'关键字是否在标题中
if 'python' in driver.title:
    print('ok')
else:
    print('no')
#6.关闭谷歌浏览器
time.sleep(3)
driver.quit()

# TODO 第一个测试案例--面向过程02
#导入模块
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
#1.打开谷歌浏览器，输入百度网址
driver = webdriver.Chrome()
time.sleep(3)
driver.get('https://www.baidu.com')
#2.获取搜索框并输入关键字--天气预报
time.sleep(3)
#定位搜索框
kw = driver.find_element(By.ID,'kw')
#输入关键字
kw.send_keys('天气预报')
#3.获取-百度一下-按钮
time.sleep(3)
su = driver.find_element(By.ID,'su')
#点击百度一下
su.click()
#4.获取页面标题，并打印出来
time.sleep(3)
print(driver.title)
#5.检查“python”关键字是否在标题中
if "python" in driver.title:
    print('ok')
else:
    print('no')
#关闭谷歌浏览器
time.sleep(3)
driver.quit()

# TODO 模拟移动端
from selenium import webdriver
import time
# 准备配置
mobileEmulation = {'deviceName':'iPhone SE'}
#创建options对象
options = webdriver.ChromeOptions()
#添加选项配置信息
options.add_experimental_option('mobileEmulation',mobileEmulation)
#打开浏览器，配置选项对象
driver = webdriver.Chrome(options=options)
#访问网址
driver.get('https://www.baidu.com')
#退出浏览器
time.sleep(3)
driver.quit()

# TODO 无头模式--浏览器在后台运行，不在界面显示
from selenium import webdriver
#选项对象，添加参数
options = webdriver.ChromeOptions()
options.add_argument('-headless')
#打开浏览器，设置选项
driver =webdriver.Chrome(options=options)
#请求百度
driver.get('https://www.baidu.com')
#输出网页标题
print(driver.title)
#退出浏览器
driver.quit()

# TODO 浏览器操作
#导入模块
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
#定义类

class BrowserOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')

    def quit(self,seconds=2):
        time.sleep(seconds)
        self.driver.quit()

     #浏览器窗口大小设置
    def window_size(self):
    #设置具体尺寸
        for i in range(3):
            time.sleep(2)
            self.driver.set_window_size(480,800)
            #浏览器最大化
            time.sleep(2)
            self.driver.maximize_window()
            #浏览器最小化
            time.sleep(2)
            self.driver.minimize_window()

    #浏览器前进和后退
    def forward_and_back(self):
        for i in range(3):
            time.sleep(2)
            self.driver.back()       #后退

            time.sleep(2)
            self.driver.forward()    #前进

    def refresh(self):
        time.sleep(2)
        self.driver.refresh()        #浏览器刷新

    # close关闭标签页
    def close(self):
        time.sleep(2)
        #连接文本定位方式获取--新闻--这个连接标签
        news = self.driver.find_element(By.LINK_TEXT,'新闻')
        news.click()
        #关闭当前标签-百度首页
        time.sleep(2)
        self.driver.close()

if __name__ == '__main__':
    browser = BrowserOperation()
    # browser.window_size()
    # browser.forward_and_back()
    # browser.refresh()
    browser.close()
    browser.quit(5)

# TODO 第一个测试案例--面向对象01
#导入模块
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
#定义类
class TestCase:
    def __init__(self):
        #初始化对象
        self.driver = webdriver.Chrome()

    def get_baidu(self):
        #请求百度
        self.driver.get('https://www.baidu.com')
        #搜索框输入内容
        time.sleep(2)
        self.driver.find_element(By.ID,'kw').send_keys('天气预报')
        #点击--百度一下
        time.sleep(2)
        self.driver.find_element(By.ID,'su').click()

    def get_so(self):
        #请求360搜索
        self.driver.get('https://www.so.com')
        #搜索框输入内容
        time.sleep(2)
        self.driver.find_element(By.ID,'input').send_keys('天气预报')
        #点击--搜索
        time.sleep(2)
        self.driver.find_element(By.ID,'search-button').click()

    def quit(self,seconds):
        #退出浏览器
        time.sleep(2)
        self.driver.quit()

if __name__ == '__main__':
    case = TestCase()
    case.get_baidu()
    # case.quit()
    #不分装的
    # time.sleep(2)
    # case.driver.quit()


# TODO 第一个测试案例--面向对象02
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()

    def get_baidu(self):
        self.driver.get('https://www.baidu.com')
        time.sleep(2)
        self.driver.find_element(By.ID,'kw').send_keys('天气预报')
        time.sleep(2)
        self.driver.find_element(By.ID,'su').click()
    def get_so(self):
        self.driver.get('https://www.so.com')
        time.sleep(2)
        self.driver.find_element(By.ID,'input').send_keys('天气预报')
        time.sleep(2)
        self.driver.find_element(By.ID,'search-button').click()
    def quit(self,seconds):
        time.sleep(seconds)
        self.driver.quit()

if __name__ == '__main__':
    case = TestCase()
    case.get_baidu()
    #case.quit()
    #不分装
    # time.sleep(2)
    # case.driver.quit()


# TODO 第一个测试案例--面向对象03
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()

    def get_baidu(self):
        self.driver.get('https://www.baidu.com')
        time.sleep(2)
        self.driver.find_element(By.ID,'kw').send_keys('天气预报')
        time.sleep(2)
        self.driver.find_element(By.ID,'su').click()

    def get_so(self):
        self.driver.get('https://www.so.com')
        time.sleep(2)
        self.driver.find_element(By.ID,'imput').send_keys('天气预报')
        time.sleep(2)
        self.driver.find_element(By.ID,'search-button').click()

    def quit(self,seconds=2):
        time.sleep(seconds)
        self.driver.quit()

if __name__ == '__main__':
    case = TestCase()
    case.get_baidu()
    # case.quit()
    #不分装
    # time.sleep(2)
    # case.driver.quit()























